package uns.fit.gis.ui;

import java.io.IOException;

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.Ticker;
import javax.microedition.lcdui.game.Sprite;

import uns.fit.gis.MapDrawer;
import uns.fit.gis.Util;
import uns.fit.gis.audio.Sound;
import uns.fit.gis.mapdrawer.MapCanvas;

public class BestRouteList2 extends Form implements CommandListener {
	private Sprite turningSprite;
	private Command backCommand;
	private Command readCommand;
	private MapDrawer midlet;
        private RouteItem[] routeItems;
	
	public BestRouteList2(MapDrawer midlet) {
		super("");
		this.midlet = midlet;
		backCommand = new Command("Quay lại", Command.BACK, 1);
		addCommand(backCommand);
		readCommand = new Command("Đọc", Command.ITEM, 1);
		addCommand(readCommand);
		setCommandListener(this);
		try {
                      turningSprite = new Sprite(Image.createImage("/turnicon.png"), 16, 16);
		}
		catch(IOException exception) {
                        exception.printStackTrace();
		}
	}
	
	public void commandAction(Command command, Displayable displayable) {
		if (command == backCommand) {
                       midlet.showMapCanvas();
		}
		else if(command == readCommand) {
			if(MapCanvas.alg != null) {
				String route = MapCanvas.alg.routeToSoundString();
				String[] streets = Util.split(route, "\n");
				Sound.readString(streets);
			}
		}
	}
	
	public void createRoute() {
		try {
		this.deleteAll();
		if(MapCanvas.alg != null) {
			String route = MapCanvas.alg.routeToString();
                        System.out.println("asdfgh");
                        System.out.println(route);
			Image img = Image.createImage(16, 16);
			Graphics g = img.getGraphics();
			if(route != null) {
				this.setTicker(new Ticker("Quãng đường tổng cộng: " + MapCanvas.alg.getTotalLength() + "m"));
				String[] subroutes = Util.split(route, "\n");
				routeItems = new RouteItem[subroutes.length / 2];

				for(int i = 0; i < subroutes.length / 2; i++){
                                        int height = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN, Font.SIZE_SMALL).getHeight() * 2 + 6;
					if(subroutes[i * 2 + 1].equalsIgnoreCase(Util.GO_STRAIGHT))
						turningSprite.setFrame(1);
					else if(subroutes[i * 2 + 1].equalsIgnoreCase(Util.TURN_LEFT))
						turningSprite.setFrame(0);
					else if (subroutes[i * 2 + 1].equalsIgnoreCase(Util.TURN_RIGHT))
						turningSprite.setFrame(2);
                                        turningSprite.setPosition(0, 0);
                                        turningSprite.paint(g);
                                        routeItems[i] = new RouteItem(turningSprite, subroutes[i * 2], subroutes[i * 2 + 1]);
                                        routeItems[i].setSize(getWidth(), height);
                                        this.append(routeItems[i]);
				}
			}
		}
		}
		catch(Exception e)  {
			e.printStackTrace();
		}
	}
}
